package com.pinyougou.service;

import com.pinyougou.pojo.TbSeller;
import com.pinyougou.sellergoods.service.SellerService;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.util.ArrayList;
import java.util.List;

public class UserDetailsServiceImpl implements UserDetailsService {
    private SellerService sellerService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        if (username == null) {
            return null;
        } else {
            TbSeller seller = sellerService.findOne(username);
            if (seller == null) {
                return null;
            } else {
                if (!"1".equals(seller.getStatus())) {
                    return null;
                } else {
                    List<SimpleGrantedAuthority> grantedAuthority = new ArrayList<>();
                    grantedAuthority.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
                    User user = new User(username, seller.getPassword(), grantedAuthority);
                    return user;
                }
            }
        }
    }

    public void setSellerService(SellerService sellerService) {
        this.sellerService = sellerService;
    }

    public SellerService getSellerService() {
        return sellerService;
    }
}
